<!DOCTYPE html>
<html>
<head lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <title>上传课程</title>
    <link type="text/css" rel="stylesheet" href="__STATIC__/home/center/css/mall.css"/>
    <link rel="stylesheet" href="__STATIC__/home/js/layer/theme/default/layer.css">
    <link rel="shortcut icon" href="__STATIC__/home/index/picture/2.png" type="image/x-icon">
</head>
<body class="body_color">
<div id="child_header">
    <div class="goback"><a href="javascript:history.back(-1)"><i></i></a></div>
    <div class="current_location"><span>上传课程</span></div>
    <div class="next_operate"><a id="update" href="javascript:;"><span>保存</span></a></div>
</div>
<div class="user_info">
    <ul>
        <form id="upload_form" method="post" enctype="multipart/form-data">
            <li>
                <span>封面图片</span>

                <a href="javascript:;">
                    <img style="width: 150px;height: 100px" class="info_input coursesPhoto" src="__STATIC__/home/common/img/akari.jpg" id="photo" onclick="photo_Upload.click()">
                    <input type="file" id="photo_Upload" name="photo" onchange="fileImg()" style="visibility: hidden; position: absolute;" >
                    <input type="hidden" class="courses_photo" value="">
                </a>

            </li>
            <li><span>课程名字</span><input class="info_input courses_name" type="text" value="" placeholder="课程名字"></li>
            <li><span>课程标题</span><input class="info_input courses_title" type="text" vocab="" placeholder="课程标题"></li>
            <li><span>课程金额</span><input class="info_input courses_money" type="text" vocab="" placeholder="课程金额"></li>
            <li>
                <span>视频</span>
                <input class="info_input " type="file" name="video_Upload" onchange="video()" style="width:22%;">
                <input type="hidden" class="courses_content" value="">
            </li>
            <li>
                <span>课程类别</span>
                <select class="info_input classify_id" style="width: 16%">
                    {volist name="res" id="v"}
                    <option value="{$v.classify_id}">{$v.course_classify}</option>
                    {/volist}
                </select>
            </li>
        </form>
    </ul>
</div>
</body>
<script src='__STATIC__/home/js/jquery.min.js' type='text/javascript'></script>
<script src="__STATIC__/home/js/layer/layer.js"></script>
<script>

    //提交数据jquery
    $(function() {

        $('#update').click(function () {

            var data = {};
            data.courses_photo = $(".courses_photo").val();
            data.courses_name = $(".courses_name").val();
            data.courses_title = $(".courses_title").val();
            data.courses_money = $(".courses_money").val();
            data.courses_content = $(".courses_content").val();
            data.classify_id = $(".classify_id").val();

            if(data.courses_photo == ""){
                layer.msg('请上传封面图片!!', {icon: 2,time: 1000});
                return false;
            }
            if(data.courses_name == ""){
                layer.msg('请输入正确课程名字!!', {icon: 2,time: 1000});
                return false;
            }
            if(data.courses_title == ""){
                layer.msg('请输入正确课程标题!!', {icon: 2,time: 1000});
                return false;
            }
            var str = /^-?\d+$/;
            if(!str.test(data.courses_money)){
                layer.msg('请输入正确金额!!', {icon: 2,time: 1000});
                return false;

            }
            if(data.courses_content == ""){
                layer.msg('请上传视频!!', {icon: 2,time: 1000});
                return false;
            }


            $.post("{:url('home/Center/addCourse')}",data,function (data) {

                console.log(data);

                if(data > 0){
                    layer.msg('上传成功，等待审核...', {icon: 1,time: 1000});
                    setTimeout(function () {
                        window.location.href = "{:url('home/center/myCourse')}";
                    }, 500);

                }else if(data == 0){
                    layer.msg('登录已过期，请从新登录!!', {icon: 5,time: 1000});
                }else {
                    layer.msg('上传失败!!', {icon: 5,time: 1000});
                }
            })
        })
    })




    /*
     *上传封面
     */
    function fileImg() {

        var upload_form = $("#upload_form")[0];
        var data_obj = new FormData(upload_form);

        layer.load();
        setTimeout(function(){
            layer.closeAll('loading');
        }, 2000);

        $.ajax({
            url: "{:url('home/Center/upload')}",
            method: "POST",
            data: data_obj,
            contentType: false,//文件上传必须
            processData: false,//文件上传必须
            success: function (data) {
                var jost = JSON.parse( data );
                console.log(jost);

                if(jost != 1){

                    layer.msg('上传成功', {icon: 1,time: 2000});

                    var imgUrl = "__UPLOADS__/photo/"+jost;

                    $(".coursesPhoto").attr('src',imgUrl);
                    $(".courses_photo").attr('value',imgUrl);

                }else{
                    layer.msg('上传失败', {icon: 2,time: 2000});

                }
            }
        });
    }

    //视频上传
    function video() {
        var upload_form = $("#upload_form")[0];
        var data_obj = new FormData(upload_form);

        layer.load();
        setTimeout(function(){
            layer.closeAll('loading');
        }, 5000);

        $.ajax({
            url: "{:url('home/Center/video_Upload')}",
            method: "POST",
            data: data_obj,
            contentType: false,//文件上传必须
            processData: false,//文件上传必须
            success: function (data) {
                var jost = JSON.parse( data );
                console.log(jost);

                if(jost != 1){

                    layer.msg('上传成功', {icon: 1,time: 2000});
                    var imgUrl = "__UPLOADS__/media/"+jost;
                    $(".courses_content").attr('value',imgUrl);

                }else{
                    layer.msg('上传失败', {icon: 2,time: 2000});

                }
            }
        });
    }

</script>
</html>